/*
 * @Author: hanxin 
 * @Date: 2021-08-13 16:43:40 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-13 22:37:44
 * 该类提供消息相关服务
 */
package service;

import java.io.ObjectOutputStream;

import common.Message;
import common.MessageType;
import common.user;
import java.util.Date;

public class messageService {
    user u = new user();

    public void sendMessage(String content, String getter, String sender) {
        try {
            if (!content.equals("exit")) {// 如果内容不是exit才发送
                Message mess = new Message();
                mess.setSender(sender);
                mess.setContent(content);
                mess.setGetter(getter);
                mess.setMessType(MessageType.MESSAGE_COMM_MES);
                mess.setSendTime(new Date().toString());

                ObjectOutputStream oos = new ObjectOutputStream(
                        manageClienToServerThread.returnThread(sender).getSocket1().getOutputStream());
                oos.writeObject(mess);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMassMassage(String content, String sender) {
        try {

            Message mess = new Message();
            mess.setSender(sender);
            mess.setContent(content);
            mess.setMessType(MessageType.MESSAGE_COMM_MES_TOALL);
            mess.setSendTime(new Date().toString());

            ObjectOutputStream oos = new ObjectOutputStream(
                    manageClienToServerThread.returnThread(sender).getSocket1().getOutputStream());
            oos.writeObject(mess);

        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }
}
